package classmate.view;

import java.awt.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

import classmate.controller.ClassmateControllerListener;
import classmate.controller.RoomController;

import classmate.po.Student;

public class StudentFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	private ClassmateControllerListener controllerListener;
	private RoomController roomController;
	private JScrollPane tablePanel;
	private int selRow = -1;//选中的表格行索引
	public StudentFrame(){
		this.controllerListener = new ClassmateControllerListener();
		this.controllerListener.setClassmateFrame(this);
		this.roomController = new RoomController();
		this.roomController.setClassmateFrame(this);
		this.setJMenuBar(getStudentMenuBar());
		this.add(this.getStudenToolBar(),BorderLayout.SOUTH);
		this.add(this.getStudentTablePanel(controllerListener.getClassmateService().findAllStudent()));
		this.setVisible(true);
		this.setSize(1000, 800);
		this.setResizable(false);
		this.setLocationRelativeTo(null);//设置null可以将该窗口位置放置在屏幕中间
		this.validate();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.validate();
	
	}

	
	/**
	 * 加载数据表格面板
	 */
	public void refreshStudent(List<Student> studentlList) {
		System.out.println("测试——当前学生数量为："+studentlList.size());
		this.remove(tablePanel);
		this.add(this.getStudentTablePanel(controllerListener.getClassmateService().findAllStudent()),BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功");
	}
	
	
	public void showStudentSearchResult(List<Student> result) {
		this.remove(tablePanel);
		this.add(this.getStudentTablePanel(result) ,BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");
	}
	
	
	/**菜单栏
	 * @return
	 */
	private JMenuBar getStudentMenuBar() {
		JMenu fileMenu = new JMenu("学生管理");
		JMenuBar bar = new JMenuBar();
		JMenuItem addStudentMenuItem = new JMenuItem("添加学生");
		JMenuItem searchStudentMenuItem = new JMenuItem("查询学生");
		JMenuItem listStudentMenuItem = new JMenuItem("学生总览");
		addStudentMenuItem.addActionListener(controllerListener);
		addStudentMenuItem.setActionCommand("addStudentDialog");
		searchStudentMenuItem.addActionListener(controllerListener);
		searchStudentMenuItem.setActionCommand("searchStudentDialog");
		listStudentMenuItem.addActionListener(controllerListener);
		listStudentMenuItem.setActionCommand("refreshStudentFrame");
		fileMenu.add(addStudentMenuItem);
		fileMenu.add(searchStudentMenuItem);
		fileMenu.add(listStudentMenuItem);
		bar.add(fileMenu);
		
		JMenu menu = new JMenu("宿舍管理");
		JMenuItem addRooMenuItem = new JMenuItem("添加宿舍");
		addRooMenuItem.addActionListener(roomController);
		addRooMenuItem.setActionCommand("addRoomDialog");
		menu.add(addRooMenuItem);
		bar.add(menu);
		return bar;
	}

	
	private JToolBar getStudenToolBar() {
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);//不可移动工具栏
		toolBar.setBorder(new BevelBorder(BevelBorder.RAISED,Color.BLACK,Color.CYAN));//设置边框外观
		toolBar.setAlignmentX(LEFT_ALIGNMENT);//设置垂直对齐方式
		JButton addStudentButton = new JButton("添加学生信息");
		ImageIcon addIcon = new ImageIcon("res/add.png");
		
		addStudentButton.setIcon(addIcon);
		JButton searchStudentButton = new JButton("查询学生信息");
		ImageIcon searchIcon = new ImageIcon("res/search.png");
		searchStudentButton.setIcon(searchIcon);
		JButton updateStudentButton = new JButton("修改学生信息");
		ImageIcon updateIcon = new ImageIcon("res/update.png");
		updateStudentButton.setIcon(updateIcon);
		JButton deleteStudentButton = new JButton("删除学生信息");
		ImageIcon deleteIcon = new ImageIcon("res/delete.png");
		deleteStudentButton.setIcon(deleteIcon);
		
		addStudentButton.addActionListener(controllerListener);
		searchStudentButton.addActionListener(controllerListener);
		updateStudentButton.addActionListener(controllerListener);
		deleteStudentButton.addActionListener(controllerListener);
		
		addStudentButton.setActionCommand("addStudentDialog");
		searchStudentButton.setActionCommand("searchStudentDialog");
		updateStudentButton.setActionCommand("updateStudentDialog");
		deleteStudentButton.setActionCommand("deleteStudent");
		
		toolBar.add(addStudentButton);
		toolBar.add(new JToolBar.Separator());//创建一个新的工具栏分隔符，使其具有当前外观定义的默认大小
		toolBar.add(searchStudentButton);
		toolBar.add(new JToolBar.Separator());
		toolBar.add(updateStudentButton);
		toolBar.add(new JToolBar.Separator());
		toolBar.add(deleteStudentButton);
		toolBar.add(new JToolBar.Separator());
		return toolBar;
	}
	
	private JScrollPane getStudentTablePanel(List<Student> studentlList) {
		String titles[] = {"学号","姓名","出生日期","手机号码","电子邮箱","籍贯","家庭住址","宿舍"};
		Object data[][] = new Object[studentlList.size()][9];
		Iterator<Student> iter = studentlList.iterator();
		int i = 0;
		while(iter.hasNext()) {
			Student student = iter.next();
			data[i][0] = student.getId();
			data[i][1] = student.getName();
			data[i][2] = student.getBirthday();
			data[i][3] = student.getPhoneNumber();
			data[i][4] = student.getEmail();
			data[i][5] = student.getNativePlace();
			data[i][6] = student.getAddress();
			data[i][7] = student.getRoom().getRoomName();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data,titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("微软雅黑",Font.BOLD,18));
		table.setFont(new Font("微软雅黑",Font.PLAIN,16));
		table.getTableHeader().setPreferredSize(new Dimension(3,40));
		table.setRowHeight(table.getRowHeight()+18); //表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //表格只能选择一行
		table.repaint();
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				selRow = table.getSelectedRow();  //得到选中的行
				long id = Long.parseLong(table.getValueAt(selRow, 0).toString().trim());
				String name = table.getValueAt(selRow, 1).toString().trim();
				Date birthday = (Date) table.getValueAt(selRow, 2);
				String phoneNumber = table.getValueAt(selRow, 3).toString().trim();
				String email = table.getValueAt(selRow, 4).toString().trim();
				String nativePlace = table.getValueAt(selRow, 5).toString().trim();
				String address = table.getValueAt(selRow, 6).toString().trim();
				String roomName = table.getValueAt(selRow, 7).toString().trim();
				System.out.println("id="+id+",name="+name+",birthday="+birthday+"phoneNumber="+phoneNumber+"email="+email+"nativePlace="+nativePlace+"address="+address+"room="+roomName);
			}
		});
		tablePanel = new JScrollPane(table);
		return tablePanel;
	}
	
	public ClassmateControllerListener getControllerListener() {
		return controllerListener;
	}
	public RoomController getRoomControllerListener() {
		return roomController;
	}
	public int getSelRow() {
		return selRow;
	}
	
}
